/**
 * @author Samsung
 */

var GData = {
   profileList: new Array(),
   suggestionList: new Array(),
   profiles : new Array(),
   currentProfile : 'New Profile'
   
   
}

function profileListBlurCallback(index)
{
    alert(index + ' item selected');
   
    if (GData.profiles[GData.profileList[index]] != undefined)
    {
        var profile = GData.profiles[GData.profileList[index]];
        transferDataFromControls();
        ServerProxy.sendProfile(profile);
        
    }
    else
    {
       // ServerProxy.getProfile(GData.profileList[index]);
    }
}

function profileListFocusCallback(index)
{
    alert(index + ' item selected');
    GData.currentProfile = GData.profileList[index];
    if (GData.profiles[GData.profileList[index]] != undefined)
    {
        alert('Retiving info from GData (' + GData.profileList[index] +')');
        var profile = GData.profiles[GData.profileList[index]];
        transferDataToControls();
    }
    else
    {
        alert('Loading profile data for ' +GData.profileList[index]);
        ServerProxy.getProfile(GData.profileList[index]);
    }
}



function transferDataToControls()
{
    var profile = GData.profiles[GData.currentProfile];
    if (profile != null)
    {
        var tab = GlobalContainer[0].objects[0][1].objects[1][0];
        tab.objects[0][0].setValue(profile.MovieBrutality);
        tab.objects[1][0].setValue(profile.MovieRomance);
        tab.objects[2][0].setValue(profile.MovieHumor);
            
        tab = GlobalContainer[0].objects[0][1].objects[1][1];
        tab.objects[0][0].setValue(profile.SerialBrutality);
        tab.objects[1][0].setValue(profile.SerialRomance);
        tab.objects[1][0].setValue(profile.SerialHumor);
        
        
    }
    
}

function transferDataFromControls()
{
    var profile = GData.profiles[GData.currentProfile];
    if (profile != null)
    {
        var tab = GlobalContainer[0].objects[0][1].objects[1][0];
        profile.MovieBrutality = tab.objects[0][0].value;
        profile.MovieRomance = tab.objects[1][0].value;
        profile.MovieHumor = tab.objects[2][0].value;
            
        tab = GlobalContainer[0].objects[0][1].objects[1][1];
        profile.SerialBrutality = tab.objects[0][0].value;
        profile.SerialRomance = tab.objects[1][0].value;
        profile.SerialHumor = tab.objects[1][0].value;
    }
    
}    
         

function createProfile()
{
    //show input
    alert('creating profile');
    profileName = "ala";
    ind = 0;
    n = profileName;
    while (GData.profileList.indexOf(n) != -1)
    {
        n = profileName + (++ind);
        alert('trying profile ' + profileName);
    }
    profileName = n;
        
    prof = new Profile();
    prof.name = profileName;
    GData.profileList.push(profileName);
    GData.profiles[profileName] = prof;
    GlobalContainer[0].objects[0][0].refreshData(GData.profileList);
    GlobalContainer[0].objects[0][0].setIndex(GData.profileList.length-1);
    alert('created profile ' + profileName);
}
function deleteProfile()
{
    alert('deleting profile');
    ind = GlobalContainer[0].objects[0][0].getIndex();
    printObject(GData.profileList);
    profileName = GData.profileList.splice(ind,1);
    ServerProxy.removeProfile(profileName);
    GlobalContainer[0].objects[0][0].refreshData(GData.profileList);
    if (ind  == GData.profileList.length)
        GlobalContainer[0].objects[0][0].setIndex(ind - 1);
    else
        GlobalContainer[0].objects[0][0].setIndex(ind);
    delete GData.profiles[profileName];
    alert('Profle ' + profileName + ' deleted');
}  
    


CodeRepo.SetReady('GData');